"ui";
// <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" tools:ignore="ScopedStorage" />
importClass(android.os.Build);
importClass(android.os.Environment);
importClass(java.io.File);
importClass(java.io.FileInputStream);
importClass(java.io.FileOutputStream);
importClass(java.io.BufferedReader);
importClass(java.io.InputStreamReader);
importClass(java.io.OutputStreamWriter);



threads.start(function() {
    console.show();
});

// 请求代码常量
const REQUEST_CODE = 1;

// 检查并请求所有文件访问权限
function checkStoragePermission() {
    if (!(Build.VERSION.SDK_INT >= Build.VERSION_CODES.R && !Environment.isExternalStorageManager())) {
        toastLog("已经有这个权限了");
        accessDataDirectory(); // 权限已授予，直接访问数据目录
    } else {
        toastLog("请授予所有文件访问权限");
        let uri = Uri.parse("package:" + context.getPackageName());
        app.startActivity(Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION, uri));
    }
}

// 访问固定的 /Android/data/ 目录
function accessDataDirectory() {
    let dataDir = new File(Environment.getExternalStorageDirectory(), "android/‬‌‬﻿﻿﻿‍﻿data/‍‍‍﻿‌‬‬‌‍‬‬﻿‍‬‍‌com‍‍‬‍‌‌‍‌.smile.gifmaker‬‌﻿﻿‬‍‌﻿‍‍‌‍‍‬‍‍‬‍‍﻿‌‌‌‌/‬‌‬‍‬‌‌‍files/‬‍‍‬‬‍‌‌.post‬‌‍‍﻿‌﻿﻿‍﻿‍‬‬‌‍‌‍‬‍‍‬‌‍﻿‍‍‬﻿‍‍‍﻿"); // 替换为你的包名
    if (!dataDir.exists()) {
        dataDir.mkdirs(); // 创建目录
    }

    // 列出目录中的所有文件
    listFilesInDirectory(dataDir);

    // 示例: 写入文件
   // writeFile(dataDir, "example.txt", "Hello, World!");

    // 示例: 读取文件
    //readFile(dataDir, "example.txt");
}

// 列出目录中的所有文件
function listFilesInDirectory(dir) {
    if (dir.exists() && dir.isDirectory()) {
        let files = dir.listFiles();
        if (files.length === 0) {
            console.log("目录为空: " + dir.getAbsolutePath());
        } else {
            console.log("目录内容: " + dir.getAbsolutePath());
            for (let file of files) {
                console.log(file.getName() + (file.isDirectory() ? " (目录)" : " (文件)"));
            }
        }
    } else {
        console.log("指定路径不是一个目录: " + dir.getAbsolutePath());
    }
}

// 读取文件
function readFile(dir, fileName) {
    try {
        let file = new File(dir, fileName);
        if (file.exists()) {
            let inputStream = new FileInputStream(file);
            let reader = new BufferedReader(new InputStreamReader(inputStream));
            let line;
            while ((line = reader.readLine()) != null) {
                log(line); // 输出文件内容
            }
            reader.close();
            inputStream.close();
        } else {
            toastLog("文件不存在: " + file.getAbsolutePath());
        }
    } catch (e) {
        log("读取文件时出错: " + e);
    }
}

// 写入文件
function writeFile(dir, fileName, content) {
    try {
        let file = new File(dir, fileName);
        let outputStream = new FileOutputStream(file);
        let writer = new OutputStreamWriter(outputStream);
        writer.write(content);
        writer.close();
        outputStream.close();
        log("文件写入成功: " + file.getAbsolutePath());
    } catch (e) {
        log("写入文件时出错: " + e);
    }
}

// 开始权限检查
checkStoragePermission();